<template>
<div>
  <basic-container>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>数据</el-breadcrumb-item>
      <el-breadcrumb-item>广告</el-breadcrumb-item>
      <el-breadcrumb-item>广告位数据</el-breadcrumb-item>
    </el-breadcrumb>
  </basic-container>
  <basic-container>
    <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
    >
    <!-- 自定义按钮 -->
      <template slot-scope="" slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="small"
          plain
          @click="handleExcel()"
          >导出Excel</el-button
        >
      </template>
    </avue-crud> 
  </basic-container>
</div>
</template>

<script>
import { handleExcelCat } from "@/api/exportCommon";
import {
  getAdPositionList,
  getAdProviderList
} from "@/api/cat/ad";
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { adData as baseURL } from "@/api/cat/baseUrls";
export default {
  async created() {
    let response = await getAdPositionList();
    this.option.column[2].dicData = response.data.source;

    let {data} = await getAdProviderList();
    this.option.column[3].dicData = data.source;
  },
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10
      },
      search:{},
      loading:false,
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "statistics_date&DESC,ad_position&ASC,ad_type&DESC",
      },
      option: {
        index: true,
        indexLabel: "编号",
        searchBtn: true,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        menu: false,
        excelBtn: false,
        column: [
          {
            label: "日期",
            prop: "statistics_date_search",
            type: "daterange",
            search: true,
            searchSpan:8,
            hide:true,
          },
          {
            label: "日期",
            prop: "statistics_date",
            type: "date",
            format:"yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: false,
            sortable:true
          },
          {
            label: "广告位",
            prop: "ad_position",
            type: "select",
            dicData: [],
            props: {
              label: "advertiser_type",
              value: "id",
            }
          },
          {
            label: "广告主",
            prop: "ad_type",
            type: "select",
            dicData: [],
            props: {
              label: "ad_provider_name",
              value: "ad_type",
            }
          },
          {
            label: "请求PV",
            prop: "request_pv",
            type: "input",
            sortable:true
          },
          {
            label: "请求UV",
            prop: "request_uv",
            type: "input",
            sortable:true
          },
          {
            label: "广告返回成功PV",
            prop: "exposure_pv",
            type: "input",
            sortable:true,
            width:"140px"
          },
          {
            label: "广告返回成功UV",
            prop: "exposure_uv",
            type: "input",
            sortable:true,
            width:"140px"
          },
          {
            label: "曝光PV",
            prop: "actual_display_pv",
            type: "input",
            sortable:true
          },
          {
            label: "曝光UV",
            prop: "actual_display_uv",
            type: "input",
            sortable:true
          },
          {
            label: "点击PV",
            prop: "click_pv",
            type: "input",
            sortable:true
          },
          {
            label: "点击UV",
            prop: "click_uv",
            type: "input",
            sortable:true
          },
        ],
      },
    };
  },
  methods: {
    //数据导出
     async handleExcel() {
       this.loading = true;
       //处理搜索条件
       let params = {...this.$refs.crud.searchForm};
       if(params.statistics_date_search.length>0){
          params.statistics_date = `${params.statistics_date_search.length>0 ? "!<>!" + params.statistics_date_search[0]+","+ params.statistics_date_search[1] : ""}`
       }
       delete params.statistics_date_search;

       //调用导出接口
       handleExcelCat(
         this.$export,
         baseURL.getAdList,
         { ...params, ...this.default },
         "广告位数据",
         this.page.total,
         [...this.option.column],
         {
           handleIndexArr:[2,3],
           selectNameArr:["ad_position","ad_type"],
           sourceNameArr:["id","ad_type"],
           sourceValueArr:["advertiser_type","ad_provider_name"]
         }
       );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.getAdList,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    } /**刷新 */,
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      
      params.statistics_date = `${params.statistics_date_search ? "!<>!" + params.statistics_date_search : ""}`
      delete params.statistics_date_search;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>
 